define(function(require,exports,module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	var easyutil = require("resource/js/component/easyutil.js");
	require("resource/js/ueditor/umeditor.config.js");
	require("resource/js/ueditor/umeditor.js");
	require("resource/js/ueditor/themes/default/css/umeditor.css");
	require("resource/js/ueditor/jquery.ueditor.js");
	
	$('<script id="editor" type="text/plain" style="width:1250px;height:130px;"></script>').insertAfter($("#emailText"));
	var ue = UM.getEditor('editor',{
		imageUrl:BASEPATH+"/resource/js/ueditor/jsp/imageUp.jsp",
		imagePath: BASEPATH + "/"
	});
	var url = '';
	//添加
	$("#addBtn").click(function() {
		$("#emailSignBlock").hide();
		url = '/oa/email/saveEmailSign.htm';
		$(".from-block").show();
	});
	//修改
	$("#editBtn").click(function() {
		url = '/oa/email/updateEmailSign.htm';
		$("#emailSignBlock").hide();
		UM.getEditor('editor').setContent($("#emailSignBlock").html());
		$(".from-block").show();
	});
	//删除
	$("#delBtn").click(function() {
		var signId = $("#emailSignBlock").data('sign-id');
		ajax.init().success(function(code,msg,data){
			 if(code==0){
				 $.messager.show({
					title:'',
					msg:msg,
					timeout:1000,
					showType:'slide'
				 });
				 $('#editor').html('');
				 $(".from-block").hide();
				 getEmailSign();
			 }
		}).post(BASEPATH + '/oa/email/deleteEmailSign.htm', {status: 1, signId: signId});
	});
	//保存
	$(".handle-btn").click(function(){
		var signContent =  UM.getEditor('editor').getContent();
		var signId = $("#emailSignBlock").data('sign-id');
		if (signContent == null || signContent == '' || signContent.length == 0) {
			 $.messager.show({
				title:'',
				msg: "签名内容不能为空",
				timeout:1000,
				showType:'slide'
			 });
			 return;
		}
		if (UM.getEditor('editor').getContentTxt().length >= 4000) {
			 $.messager.show({
				title:'',
				msg: "签名内容不能超过4000字符",
				timeout:1000,
				showType:'slide'
			 });
			 return;
		}
		ajax.init().success(function(code,msg,data){
			 if(code==0){
				 $.messager.show({
					title:'',
					msg:msg,
					timeout:1000,
					showType:'slide'
				 });
				 $('#editor').html('');
				 $(".from-block").hide();
				 getEmailSign();
			 }
		}).post(BASEPATH + url, {signContent: signContent, signId: signId});
	});
	//取消
	$(".handle-cancel").click(function() {
		$('#editor').html('');
		$(".from-block").hide();
		$("#emailSignBlock").show();
	});
	
	function getEmailSign() {
		ajax.init().success(function(code,msg,data){
			 if(code==0){
				 if (data) {
					 $("#addBtnBlock").hide();
					 $("#editBtnBlock").show();
					 $("#delBtnBlock").show();
					 if (data.signContent == null || data.signContent == '') {
						 $("#emailSignBlock").html('暂无签名').show();
					 } else {
						 $("#emailSignBlock").html(data.signContent).show();
					 }
					 $("#emailSignBlock").data('sign-id', data.signId);
				 } else {
					 $("#emailSignBlock").html('暂无签名').show();
					 $("#addBtnBlock").show();
					 $("#editBtnBlock").hide();
					 $("#delBtnBlock").hide();
				 }
			 }
		}).post(BASEPATH + "/oa/email/getEmailSign.htm");
	}
	
	module.exports = {
		init: function() {
			getEmailSign();
		}
	}
});
